<!DOCTYPE HTML>
<html lang="zh-CN">

<head><meta name="generator" content="Hexo 3.9.0">
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="ydong博客">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    <meta name="browsermode" content="application">
    <meta name="screen-orientation" content="portrait">
    <meta name="theme-version" content="1.2.3">
    <meta name="root" content="/blog/">
    <link rel="dns-prefetch" href="http://ydongabc.gitee.io/blog">
    <!--SEO-->

<meta name="keywords" content="表单,按需展示">


<meta name="description" content="功能：按上一条表单项的所填值 来决定下一条表单项是否展示与隐藏 以及 该展示哪一项
1.表单结构给每项表单项添加一个自己的id名，并用v-model绑定相对应的数据，利用v-if根据上一个表单项...">


<meta name="robots" content="all">
<meta name="google" content="all">
<meta name="googlebot" content="all">
<meta name="verify" content="all">
    <!--Title-->

<title>
    
    vue按需条件动态显示隐藏表单 |
    
    ydong博客
</title>

<link rel="alternate" href="/atom.xml" title="ydong博客" type="application/atom+xml">


<link rel="icon" href="./favicon.ico">

    

<link rel="stylesheet" href="/blog/css/bootstrap.min.css?rev=3.3.7">
<link rel="stylesheet" href="/blog/css/font-awesome.min.css?rev=4.7.0">
<link rel="stylesheet" href="/blog/css/style.css?rev=@@hash">
    
<div class="hide">
    <script type="text/javascript">
    var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");
    document.write(unescape("%3Cspan class='cnzz_stat_icon_1263868967 hide' %3E%3Cscript%20src%3D%22https%3A%2F%2Fs95.cnzz.com%2Fz_stat.php%3Fweb_id%3D1272564536%22%3E%3C%2Fscript%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/z_stat.php%3Fid%3D1263868967%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
    </script>
</div>




    

</head></html>
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<div style="ie">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->
<body>
    <header class="main-header"  style="background-image:url(
    /blog/./img/banner.jpg)"
     >
    <div class="main-header-box">
        <a class="header-avatar" href="/" title='John Doe'>
            <img src="/blog/img/avatar.jpg" alt="logo头像" class="img-responsive center-block">
        </a>
        <div class="branding">
            <!--<h2 class="text-hide">Snippet主题,从未如此简单有趣</h2>-->
            
            <img src="/blog/img/branding.png" alt="Snippet 博客主题" class="img-responsive center-block">
            
        </div>
    </div>
</header>
    <nav class="main-navigation">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="navbar-header"><span class="nav-toggle-button collapsed pull-right" data-toggle="collapse" data-target="#main-menu" id="mnav">
                        <span class="sr-only"></span>
                        <i class="fa fa-bars"></i>
                    </span>
                    <a class="navbar-brand" href="http://ydongabc.gitee.io/blog">
                        ydong博客</a>
                </div>
                <div class="collapse navbar-collapse" id="main-menu">
                    <ul class="menu">
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog"><i class="fa "></i>
                                首页</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/前端/"><i class="fa "></i>
                                前端</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/后端/"><i class="fa "></i>
                                后端</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/工具/"><i class="fa "></i>
                                工具</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/archives/"><i class="fa "></i>
                                时间轴</a>
                        </li>
                        
                    </ul>
                </div>
            </div>
        </div>
    </div>
</nav>
    <section class="content-wrap">
        <div class="container">
            <div class="row">
                <main class="col-md-8 main-content m-post">
                    <p id="process"></p>
<article class="post">
    <div class="post-head">
        <h1 id="vue按需条件动态显示隐藏表单">
            
            vue按需条件动态显示隐藏表单
            
        </h1>
        <div class="post-meta">
    
    <span class="categories-meta fa-wrap">
        <i class="fa fa-folder-open-o"></i>
        <a class="category-link" href="/blog/categories/前端/">前端</a> <a class="category-link" href="/blog/categories/前端/vue/">vue</a>
    </span>
    
    
    <span class="fa-wrap">
        <i class="fa fa-tags"></i>
        <span class="tags-meta">
            
            <a class="tag-link" href="/blog/tags/按需展示/">按需展示</a> <a class="tag-link" href="/blog/tags/表单/">表单</a>
            
        </span>
    </span>
    
    
    
    <span class="fa-wrap">
        <i class="fa fa-clock-o"></i>
        <span class="date-meta">
            2019/11/06</span>
    </span>
    
    
</div>
        
        
    </div>
    
    <div class="post-body post-content">
        <p>功能：按上一条表单项的所填值 来决定下一条表单项是否展示与隐藏 以及 该展示哪一项</p>
<h2 id="1-表单结构"><a href="#1-表单结构" class="headerlink" title="1.表单结构"></a>1.表单结构</h2><p>给每项表单项添加一个自己的id名，并用v-model绑定相对应的数据，利用v-if根据上一个表单项的数据值来进行显示或隐藏</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">form</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">form-item</span> <span class="attr">id</span>=<span class="string">"a1"</span> <span class="attr">v-model</span>=<span class="string">"data1"</span>&gt;</span>表单1<span class="tag">&lt;/<span class="name">form-item</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">form-item</span> <span class="attr">id</span>=<span class="string">"a2"</span> <span class="attr">v-model</span>=<span class="string">"data2"</span> <span class="attr">v-if</span>=<span class="string">"data1=='条件值'"</span>&gt;</span>表单2<span class="tag">&lt;/<span class="name">form-item</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">form-item</span> <span class="attr">id</span>=<span class="string">"a3"</span> <span class="attr">v-model</span>=<span class="string">"data3"</span> <span class="attr">v-if</span>=<span class="string">"data2=='条件值'"</span>&gt;</span>表单3<span class="tag">&lt;/<span class="name">form-item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line">data()&#123;</span><br><span class="line"><span class="actionscript">    <span class="keyword">return</span> &#123;</span></span><br><span class="line">        formdata: &#123;</span><br><span class="line"><span class="actionscript">            data1: <span class="string">''</span>, </span></span><br><span class="line"><span class="actionscript">            data2: <span class="string">''</span>, </span></span><br><span class="line"><span class="actionscript">            data3: <span class="string">''</span></span></span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>注：<br>此时虽然可以实现按某个表单项的值来选择是否显示另一个表单项，<br>但表单3是依据表单2的数据显示的话，当将表单2显示后再隐藏时，<br>表单3并不会跟着隐藏，因为表单2绑定的数据值还存在，并没有被清空，所以我们需要手动清空一下</p>
<h2 id="2-数据清空方法"><a href="#2-数据清空方法" class="headerlink" title="2.数据清空方法"></a>2.数据清空方法</h2><p>在data中添加一个数组，用来存放表单项的id名</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">data()&#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        hidden: [<span class="string">'a2'</span>,<span class="string">'a3'</span>], <span class="comment">//存放需隐藏的表单项id</span></span><br><span class="line">        formdata: &#123;</span><br><span class="line">            data1: <span class="string">''</span>, </span><br><span class="line">            data2: <span class="string">''</span>, </span><br><span class="line">            data3: <span class="string">''</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后创建方法，检测该表单项是否已隐藏移除(即先前添加的id名将会不存在)，<br>将v-if隐藏的表单项的数据值清空，即可隐藏先前显示的表单项，避免表单项错误重复显示<br>在生命周期函数updated(数据更新后)中，遍历hidden数组将不存在的表单项数据清空</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">updated()&#123;  </span><br><span class="line">    <span class="keyword">this</span>.hidden.forEach(<span class="function"><span class="params">id</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (!<span class="built_in">document</span>.getElementById(id)) &#123;</span><br><span class="line">        <span class="keyword">this</span>.formdata[id] = <span class="string">""</span>;	<span class="comment">//注：不能写this.formdata.id=''，这样js会把id作为一个新的属性值，而不是匹配</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>
    
    <div class="post-footer">
        <div>
            
            转载声明：
            商业转载请联系作者获得授权,非商业转载请注明出处 © <a href="" target="_blank">Snippet</a>
            
            
        </div>
        <div>
            
        </div>
    </div>
</article>
<div class="article-nav prev-next-wrap clearfix">
    
    <a href="/blog/2019/11/07/H5全屏滚动活动页插件/" class="pre-post btn btn-default" title='H5全屏滚动活动页插件'>
        <i class="fa fa-angle-left fa-fw"></i><span class="hidden-lg">上一篇</span>
        <span class="hidden-xs">
            H5全屏滚动活动页插件</span>
    </a>
    
    
    <a href="/blog/2019/11/05/elementUI中的表单相关的坑/" class="next-post btn btn-default" title='elementUI中的表单相关的坑'>
        <span class="hidden-lg">下一篇</span>
        <span class="hidden-xs">
            elementUI中的表单相关的坑</span><i class="fa fa-angle-right fa-fw"></i>
    </a>
    
</div>

                </main>
                
                    <aside id="article-toc" role="navigation" class="col-md-4">
    <div class="widget">
        <h3 class="title">
            文章目录
        </h3>
        
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-表单结构"><span class="toc-text">1.表单结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-数据清空方法"><span class="toc-text">2.数据清空方法</span></a></li></ol>
        
    </div>
</aside>
                
            </div>
        </div>
    </section>
    <footer class="main-footer">
    <div class="container">
        <div class="row">
        </div>
    </div>
</footer>
<a id="back-to-top" class="icon-btn hide">
    <i class="fa fa-chevron-up"></i>
</a>
    <div class="copyright">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="busuanzi">
    
</div>
            </div>
            <div class="col-sm-12">
                <span>Copyright &copy;
                    2017
                </span> |
                <span>
                    Powered by <a href="//hexo.io" class="copyright-links" target="_blank" rel="nofollow">Hexo</a>
                </span> |
                <span>
                    Theme by <a href="//github.com/shenliyang/hexo-theme-snippet.git" class="copyright-links" target="_blank" rel="nofollow">Snippet</a>
                </span>
            </div>
        </div>
    </div>
</div>



<script src="/blog/js/app.js?rev=@@hash"></script>
</body>
</html>